package com.alibaba.citrus.cr.mn.order.center.facade.function.check;

import com.alibaba.citrus.cr.common.enums.ordercenter.DictionaryTypeEnum;
import com.alibaba.citrus.cr.common.exception.FunctionException;
import com.alibaba.citrus.cr.mn.order.center.common.util.AssertUtils;
import com.alibaba.citrus.cr.mn.order.center.facade.adapter.BaseDataServiceAdapter;
import com.epoch.app.mnordercenter.model.dto.DictionaryResponse;
import com.epoch.app.mnordercenter.model.dto.OrderAtomSubmitResponse;
import com.epoch.app.mnordercenter.orderbaserequest.OrderAtomSubmitRequest;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * @author dongju
 * @ClassName ExchangeOrderSubmitParamsCheck
 * @description: 批量提交换货订单入参校验
 * @date 2023/05/21
 * @version: 1.0
 */
@Service
public class ExchangeOrderSubmitParamsBizCheck extends AbstractOrderParamsCheckService<OrderAtomSubmitRequest, OrderAtomSubmitResponse> {

    @Resource
    private BaseDataServiceAdapter baseDataServiceAdapter;
    @Override
    public void paramsEnumDictCheck(List<OrderAtomSubmitRequest> requestList, List<OrderAtomSubmitResponse> responseList){

        // 系统来源
        List<DictionaryResponse> fromSysResponseList = baseDataServiceAdapter.queryDictionarys(DictionaryTypeEnum.FROM_SYS.getCode());
        Map<String, DictionaryResponse> fromSysMap = fromSysResponseList.stream().collect(Collectors.toMap(DictionaryResponse::getCode, Function.identity(), (k1, k2) -> k2));
        //业态
        List<DictionaryResponse> businessTypeResponseList = baseDataServiceAdapter.queryDictionarys(DictionaryTypeEnum.BUSINESS_TYPE.getCode());
        Map<String, DictionaryResponse> businessTypeMap = businessTypeResponseList.stream().collect(Collectors.toMap(DictionaryResponse::getCode, Function.identity(), (k1, k2) -> k2));

        requestList.forEach(t->{
            if (fromSysMap.get(t.getFromSys()) == null) {
                throw new FunctionException("OC-01-001-01-15-006", "来源系统编码");
            }
            if (businessTypeMap.get(t.getBusinessType()) == null) {
                throw new FunctionException("OC-01-001-01-15-006", "业态编码");
            }
        });
    }

    @Override
    public void paramsNotBlankCheck(List<OrderAtomSubmitRequest> requestList, List<OrderAtomSubmitResponse> responseList) {
        AssertUtils.isTrue(CollectionUtils.isEmpty(requestList), "OC-01-001-01-15-001", "单据列表");
        AssertUtils.isTrue(requestList.size() > 100, "OC-01-001-01-15-004", "单据列表", requestList.size(), "100");
        requestList.forEach(t -> {
            // 必填校验
            AssertUtils.isTrue(StringUtils.isBlank(t.getFromSys()),              "OC-01-001-01-15-001", "来源系统编码");
            AssertUtils.isTrue(StringUtils.isBlank(t.getBusinessType()),         "OC-01-001-01-15-001", "业态编码");
            AssertUtils.isTrue(StringUtils.isBlank(t.getBusinessUnitCode()),     "OC-01-001-01-15-001", "业务单元编码");
            AssertUtils.isTrue(StringUtils.isBlank(t.getSalesOrderNo()),     "OC-01-001-01-15-001", "换货订单号");

            AssertUtils.isTrue(StringUtils.isBlank(t.getModifierCode()), "OC-01-001-01-15-001", "修改人工号");
            List<String> sameSourceOrderNos = requestList.stream().map(OrderAtomSubmitRequest::getSalesOrderNo)
                    .collect(Collectors.toMap(x -> x, x -> 1, Integer::sum))
                    .entrySet().stream().filter(entry -> entry.getValue() > 1)
                    .map(Map.Entry::getKey).collect(Collectors.toList());
            if (CollectionUtils.isNotEmpty(sameSourceOrderNos)) {
                throw new FunctionException("OC-01-001-01-15-002", "换货订单单号", StringUtils.join(sameSourceOrderNos, ","));
            }
        });
    }

    @Override
    public void paramsLengthCheck(List<OrderAtomSubmitRequest> requestList, List<OrderAtomSubmitResponse> responseList) {
        requestList.forEach(t -> {
            AssertUtils.isNumber(t.getBusinessType(), true, "OC-01-001-01-15-006", "业态编码");
            // 必填校验
            AssertUtils.lengthCheck(t.getFromSys() ,         50, true,  "OC-01-001-01-15-004", "来源系统编码");
            AssertUtils.lengthCheck(t.getBusinessType(),     10, true,  "OC-01-001-01-15-004", "业态编码");
            AssertUtils.lengthCheck(t.getBusinessUnitCode(), 50, true,  "OC-01-001-01-15-004", "业务单元编码");
            AssertUtils.lengthCheck(t.getSalesOrderNo(), 50, true,  "OC-01-001-01-15-004", "订单单号");

            AssertUtils.lengthCheck(t.getModifierCode(), 50, true, "OC-01-001-01-15-004", "修改人工号");

        });
    }
}
